/*
 * Releases.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.changelog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.content.Context;

public class Changelog {
	
	private ArrayList<Release> mReleases = new ArrayList<Release>();
		
	private XMLParsing mXMLParsing;
	private IItemsUpdateListener mUpdateListener;
	private ChangelogListAdapter ila;
	private Context mCtx;
	
	public Changelog( Context ctx ) {
		mXMLParsing = new XMLParsing();
		mCtx = ctx;
	}
	
	public void setContext( Context ctx ) {
		mCtx = ctx;
	}
	
	public void setUpdateListener( IItemsUpdateListener updateListener ) {
		mUpdateListener = updateListener;
	}
	
	public ChangelogListAdapter getChangelogListAdapter() {
		if ( ila == null )
			ila = new ChangelogListAdapter( mCtx, this );
		return ila;
	}
	
	public void setReleases( ArrayList<Release> items ) {
		mReleases = items;
	}
	
	public void addRelease( Release item ) {
		mReleases.add( item );
	}
	
	public Release getRelease( int index ) {
		if ( index < 0 || index >= mReleases.size())
			throw new IndexOutOfBoundsException();
		return mReleases.get( index );
	}
	
	public int getSize() {
		return mReleases.size();
	}
	
	public void clear() {
		mReleases.clear();
		if ( mUpdateListener != null )
			mUpdateListener.notifyUpdate();
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for( Release r: mReleases) {
			sb.append( r.toString());
		}
		return sb.toString();
	}
	
	public void populateItems( Context ctx ) throws SAXException, ParserConfigurationException, IOException {
		
		mReleases = mXMLParsing.parse( ctx );
		if ( mUpdateListener != null )
			mUpdateListener.notifyUpdate();
	}
	
}
